package com.qf.micrograss.grass.controller;

import cn.hutool.core.util.RandomUtil;
import com.qf.common.core.annotation.NoLogin;
import com.qf.common.core.common.PageVO;
import com.qf.common.core.common.R;
import com.qf.common.minio.utils.MinioUtils;
import com.qf.micrograss.grass.pojo.dto.GrassDTO;
import com.qf.micrograss.grass.pojo.dto.GrassSearchDTO;
import com.qf.micrograss.grass.pojo.vo.GrassVO;
import com.qf.micrograss.grass.service.IGrassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/11/10
 * @desc
 */
@RestController
@RequestMapping("/article")
public class GrassController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private IGrassService grassService;

    /**
     * 图片上传
     */
    @PostMapping("/upload/img")
    public R uploadImage(MultipartFile file) throws IOException {

        String prefix = RandomUtil.randomString(10);
        String[] arr = file.getOriginalFilename().split("\\.");
        String suffix = arr[arr.length - 1];

        String imgPath = minioUtils.upload(prefix + "." + suffix, file.getInputStream());

        return R.ok(imgPath);
    }

    /**
     * 直接发表文章
     */
    @PostMapping("/add")
    public R add(@RequestBody GrassDTO grassDTO){
        grassService.add(grassDTO);
        return R.ok();
    }

    /**
     * 定时发表文章
     */
    @PostMapping("/add2")
    public R add2(@RequestBody GrassDTO grassDTO){
        grassService.add(grassDTO);
        return R.ok();
    }

    /**
     * 分页查询种草文章
     */
    @GetMapping("/page")
    public R page(GrassSearchDTO dto){
        PageVO<GrassVO> pageVO = grassService.page(dto);

        return R.ok(pageVO);
        /**
         * {
         *  code:200,
         *  msg:"",
         *  data:{
         * 	   dataList:[],
         * 	   total:100
         *  }
         * }
         */
    }

}
